import config from '../config.js'

const uploadImage = async function(filePath) {
	return new Promise((r,j)=>{
		uni.uploadFile({
			url: `${config.hostUrl}/upload/upload/uploadImage`,
			filePath: filePath,
			formData: {},
			name:"image",
			success: (uploadFileRes) => {
				r(JSON.parse(uploadFileRes.data))
			},
			fail() {
				r({
					success:false
				})
			}
		})
	})
}

const uploadVideo = async function(filePath) {
	return new Promise((r,j)=>{
		uni.uploadFile({
			url: `${config.hostUrl}/upload/upload/videoAndPoster`,
			filePath: filePath,
			formData: {},
			name:"video",
			success: (uploadFileRes) => {
				r(JSON.parse(uploadFileRes.data))
			},
			fail() {
				r({
					success:false
				})
			}
		})
	})
}

const uploadAudio = async function(filePath) {
	return new Promise((r,j)=>{
		uni.uploadFile({
			url: `${config.hostUrl}/upload/upload/audio`,
			filePath: filePath,
			formData: {},
			name:"audio",
			success: (uploadFileRes) => {
				r(JSON.parse(uploadFileRes.data))
			},
			fail() {
				r({
					success:false
				})
			}
		})
	})
}

const requestMessage = function({ret,message,func}){
	if(ret.success){
		uni.showToast({
			title:message ?? "请求成功",
			success() {
				if(func){
					func()
				}
			}
		})
	}else{
		uni.showToast({
			title:ret.message ?? "请求失败",
			icon:"none"
		})
	}
}

function getLastMessageStr(message){
  if(!message) return '';
  let lastMessage = ''
  switch (message.type) {
    case 'text':
      lastMessage = message.content 
      break;
    case 'image':
      lastMessage = "[图片]"
      break;
    case 'voice':
      lastMessage = "[语音]"
	  break;
    case 'video':
      lastMessage = "[视频]"
	  break;
	case 'emoji':
	  lastMessage = message.content
	  break;
    default:
	  lastMessage = message.content
      break;
  }
  return lastMessage
}

const checkPermission = async function(title) {
// #ifndef H5
	return new Promise((resolve) => {
	    plus.android.requestPermissions(
	        ["android.permission.RECORD_AUDIO",  "android.permission.CAMERA"],
	        function (resultObj) {
	            if (resultObj.granted.length < 2) { 
	              uni.showToast({
	                    icon: "none",
	                    title,
	                });
	                resolve(false)
	                let timer1 = setTimeout(() => { //没有开对应的权限，打开app的系统权限管理页
	                    var Intent = plus.android.importClass("android.content.Intent");
	                    var Settings = plus.android.importClass("android.provider.Settings");
	                    var Uri = plus.android.importClass("android.net.Uri");
	                    var mainActivity = plus.android.runtimeMainActivity();
	                    var intent = new Intent();
	                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
	                    var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
	                    intent.setData(uri);
	                    mainActivity.startActivity(intent);
	                    clearTimeout(timer1)
	                    timer1 = null
	                }, 1000)
	            } else {
	                resolve(true)
	            }
	        }
	    );
	})
// #endif

// #ifdef H5
	return true
// #endif
}



export default {
	uploadImage,
	uploadVideo,
	uploadAudio,
	requestMessage,
	getLastMessageStr,
	checkPermission
}